//
//  Model.m
//  YiShi
//
//  Created by  zyn on 15/1/7.
//  Copyright (c) 2015年 maochengfang. All rights reserved.
//

#import "Model.h"
#import <Foundation/NSObjCRuntime.h>
#import <objc/runtime.h>

@implementation Model

-(NSString *)toString:(id)value
{
    if ([value isKindOfClass:[NSString class]])
        return value;
    if ([value isKindOfClass:[NSNull class]]) {
        return @"";
    }
    if (value == nil) {
        return @"";
    }
    return [NSString stringWithFormat:@"%@",value];
    
}


- (NSString *)get2Float:(NSString *)data{
//    NSNumber* tempnumber = [NSNumber numberWithDouble:[[NSString stringWithFormat:@"%.2f",
//                                                        (float)(rand()%100001)*0.001f -20] doubleValue]];
    NSString *result = [NSString stringWithFormat:@"%0.2f",[data doubleValue]];
    return result;
}

-(NSString *)get0Float:(NSString *)data{
    NSString *result = [NSString stringWithFormat:@"%0.0f",[data doubleValue]];
    return result;
}


- (NSString *)getTime:(NSString *)timestamp{
    NSString *timeStr = [NSString stringWithFormat:@"%@",timestamp];
    
    NSTimeInterval time = [timeStr doubleValue]/1000;   //时间戳为毫秒级别

    NSDate *createTime = [NSDate dateWithTimeIntervalSince1970:time];
    
    NSTimeInterval timeInterval = [createTime timeIntervalSinceNow];
    timeInterval =  -timeInterval;
    
    long temp = 0;
    NSString *result;

    
    if (timeInterval < 60) {
        result = @"刚刚";
    }
    else if ((temp = timeInterval/60)<60)
    {
        result = [NSString stringWithFormat:@"%ld分钟前",temp];
    }
    else if ((temp = temp/60) <24)
    {
        result = [NSString stringWithFormat:@"%ld小时前",temp];
    }
    else if ((temp = temp/24) <30)
    {
        result = [NSString stringWithFormat:@"%ld天前",temp];
    }
    else if  ((temp = temp/30) <12)
    {
        result = [NSString stringWithFormat:@"%ld月前",temp];
    }
    else
    {
        temp = temp/12;
        result = [NSString stringWithFormat:@"%ld年前",temp];
    }
    
    return result;

}


-(void)initWithDictionary:(NSDictionary *)dic{
//    NSArray *dicKey = [dic allKeys];
//    NSArray *keyArray = [self getPropertyNames];
//
//    NSMutableDictionary *tempDic = [[NSMutableDictionary alloc] initWithCapacity:dicKey.count];
//    for (int i = 0; i < dicKey.count; i ++) {
//        NSString *key = dicKey[i];
//        [tempDic setObject:dic[key] forKey:propertyMapDic[key]];
//    }
    
}


/* 获取model的key数组 */
- (NSArray *) getPropertyNames{
    ///存储所有的属性名称
    NSMutableArray *allNames = [[NSMutableArray alloc] init];
    ///存储属性的个数
    unsigned int propertyCount = 0;
    ///通过运行时获取当前类的属性
    objc_property_t *propertys = class_copyPropertyList([self class], &propertyCount);
    //把属性放到数组中
    for (int i = 0; i < propertyCount; i ++) {
        ///取出第一个属性
        objc_property_t property = propertys[i];
        
        const char * propertyName = property_getName(property);
        
        [allNames addObject:[NSString stringWithUTF8String:propertyName]];
    }
    ///释放
    free(propertys);
    return allNames;
}

#pragma mark -- 通过字符串来创建该字符串的Setter方法，并返回
- (SEL) creatGetterWithPropertyName: (NSString *) propertyName{
    //1.返回get方法: oc中的get方法就是属性的本身
    return NSSelectorFromString(propertyName);
}


- (void) displayCurrentModleProperty{
    //获取实体类的属性名
    NSArray *array = [self getPropertyNames];
    NSMutableArray *valueArray = [[NSMutableArray alloc]initWithCapacity:0];
    //拼接参数
    for (int i = 0; i < array.count; i ++) {
        //获取get方法
        SEL getSel = [self creatGetterWithPropertyName:array[i]];
        if ([self respondsToSelector:getSel]) {
            //获得类和方法的签名
            NSMethodSignature *signature = [self methodSignatureForSelector:getSel];
            //从签名获得调用对象
            NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
            //设置target
            [invocation setTarget:self];
            //设置selector
            [invocation setSelector:getSel];
            //接收返回的值
            NSObject *__unsafe_unretained returnValue = nil;
            //调用
            [invocation invoke];
            //接收返回值
            [invocation getReturnValue:&returnValue];
            
            [valueArray addObject:returnValue];
        }
    }
}



@end
